package algotithm.leetcode.string.test58;

/**
 * @author 25068
 * @Date 2021/9/21 10:18
 */
class Solution {

    public static void main(String[] args) {
        String s = "a";
        Solution solution = new Solution();
        int length = solution.lengthOfLastWord(s);
        System.out.println(length);
    }

    public int lengthOfLastWord(String s) {
        int n = s.length();
        int end = n - 1;
        StringBuilder sb = new StringBuilder();
        while (end >= 0) {
            if (s.charAt(end) == ' ') {
                end--;
            } else {
                while ( end >= 0 && s.charAt(end) != ' ') {
                    sb.append(s.charAt(end));
                    end--;
                }
                break;
            }
        }
        return sb.reverse().toString().length();
    }
}